package com.tubeonfire.entity;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

@PersistenceCapable
public class TubeComment {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key; // app engine key.
	@Persistent
	private String commentId;
	@Persistent
	private Text content;
	@Persistent
	private String author;
	@Persistent
	private Date punished;

	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public Text getContent() {
		return content;
	}

	public void setContent(Text content) {
		this.content = content;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Date getPunished() {
		return punished;
	}

	public void setPunished(Date punished) {
		this.punished = punished;
	}

	public String getCommentId() {
		return commentId;
	}

	public void setCommentId(String commentId) {
		this.commentId = commentId;
	}

	/*
	 * This method convert TubeComment Object to String.
	 */
	public Text toText() {
		StringBuilder content = new StringBuilder();
		content.append("<commentId>" + this.commentId + "</commentId>\n");
		content.append("<content>" + this.content + "</content>\n");
		content.append("<author>" + this.author + "</author>\n");
		content.append("<punished>" + this.punished + "</punished>\n");
		return new Text(content.toString());
	}

	/*
	 * Convert String from Appdata to TubeComment Object.
	 */
	public void transformText(String cmtString) {
		Document doc = Jsoup.parse(cmtString);
		this.commentId = doc.select("commentId").text();
		this.author = doc.select("author").text();
		Date parsed = new Date();
		try {
			SimpleDateFormat format = new SimpleDateFormat(
					"EEE MMM dd HH:mm:ss zzz yyyy");
			parsed = format.parse(doc.select("punished").text());
			this.punished = parsed;
		} catch (Exception e) {
			Calendar cal = Calendar.getInstance();
			this.punished = cal.getTime();
		}
		this.content = new Text(doc.select("content").text());		
	}

	public static void main(String[] args) {

	}
}
